﻿using System.Collections.Generic;
using UnityEngine;

namespace Game
{
    public class ScenePetEntity : SceneBattleEntity
    {
        private PetConfigData _petData;
        private Transform _spriteTransform;
        private SpriteRenderer _spriteRenderer;
        private ScenePetBehaviorComponent _behaviorComponent;

        public ScenePetEntity(int id) : base(id, SceneObjectEntityType.Pet)
        {
        }
        
        public void LoadPet(int configId)
        {
            if (_petData != null && _petData.id == configId)
            {
                return;
            }

            _petData = ConfigSystem.Instance.Get<PetConfigEntity>().GetData(configId);
            Load(_petData.url);
        }

        public override void SetFlipX(bool flip)
        {
            _flipX = flip;
            if (_isCompleted)
            {
                _spriteRenderer.flipX = flip;  
            }
        }

        public override void SetFlipY(bool flip)
        {
            _flipY = flip;
            if (_isCompleted)
            {
                _spriteRenderer.flipY = flip;
            }
        }

        public override void Update(float deltaTime)
        {
            base.Update(deltaTime);
            if (_behaviorComponent == null)
            {
                return;
            }
            _behaviorComponent.CollidedWith(null);
            if (!_behaviorComponent.IsPressed())
            {
                return;
            }
            SceneObjectFinderComponent finderComponent = GetComponent<SceneObjectFinderComponent>();
            if (finderComponent == null)
            {
                return;
            }
            float petX = position.x;
            List<SceneMonsterEntity> sceneMonsterList = finderComponent.GetSceneMonsterList();
            for (int i = 0; i < sceneMonsterList.Count; ++i)
            {
                SceneMonsterEntity sceneMonster = sceneMonsterList[i];
                if (sceneMonster.GetComponent<SceneObjectBattlePropertiesComponent>()
                    .GetProperty(SceneObjectBattlePropertyKeys.Hp) <= 0)
                {
                    continue;
                }
                float monsterX = sceneMonster.position.x;
                if (petX < monsterX && !GetFlipY() || petX > monsterX && GetFlipY())
                {
                    if (sceneMonster.IsCollided(position, bounds))
                    {
                        _behaviorComponent.CollidedWith(sceneMonster);
                        break;
                    }
                }
            }
        }

        public override void BeHit(SceneObjectEntity attackEntity)
        {
            // base.BeHit(attackEntity);
            _behaviorComponent.BeHit(attackEntity);
        }

        public override void Die()
        {
            SceneObjectFinderComponent objectFinderComponent = GetComponent<SceneObjectFinderComponent>();
            objectFinderComponent.RemovePet(this);
            base.Die();
        }

        protected override void PostInstantiate()
        {
            base.PostInstantiate();
            _spriteTransform = _contentTransform.Find("Sprite");
            _spriteRenderer = _spriteTransform.GetComponent<SpriteRenderer>();

            SceneObjectBattlePropertiesComponent propertiesComponent = GetComponent<SceneObjectBattlePropertiesComponent>();
            propertiesComponent.SetProperty(SceneObjectBattlePropertyKeys.MaxHp, 20);
            propertiesComponent.SetProperty(SceneObjectBattlePropertyKeys.Hp, 20);
            
            _behaviorComponent = AddComponent<ScenePetBehaviorComponent>();
            _behaviorComponent.Setup(this);
            
            OnPostInstantiateCompleted();
        }

        protected override void UpdateSortingLayer(int layerId)
        {
            base.UpdateSortingLayer(layerId);
            _spriteRenderer.sortingLayerID = layerId;
        }

        protected override void UpdateSortingOrder(int order)
        {
            base.UpdateSortingOrder(order);
            _spriteRenderer.sortingOrder = order;
        }
    }
}